package com.gitee.dolt.core.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.gitee.dolt.core.exception.BizException;
import com.gitee.dolt.core.exception.code.BaseExceptionCode;
import com.gitee.dolt.core.exception.code.ExceptionCode;

import java.util.Collection;
import java.util.Objects;


/**
 * 业务断言
 *
 * @author LYH
 * @see BizException
 * @since 4.0
 */
public final class BizAssert {
    private BizAssert() {
    }

    public static void ok(String msg) {
        throw new BizException(ExceptionCode.OK.getCode(), msg);
    }

    public static void fail(BaseExceptionCode exceptionCode, String msg) {
        throw new BizException(exceptionCode.getCode(), msg);
    }

    public static void fail(int code, String msg) {
        throw new BizException(code, msg);
    }

    public static void fail(BaseExceptionCode exceptionCode) {
        if (exceptionCode != null) {
            throw new BizException(exceptionCode.getCode(), exceptionCode.getMsg());
        }
        fail(ExceptionCode.BASE_VALID_PARAM.getCode(), "参数验证异常");
    }

    public static void fail(String msg) {
        if (StrUtil.isEmpty(msg)) {
            msg = "参数验证异常";
        }
        fail(ExceptionCode.BASE_VALID_PARAM.getCode(), msg);
    }

    public static void fail() {
        fail(StrUtil.EMPTY);
    }


    public static void isTrue(Boolean condition, String msg) {
        if (!BooleanUtil.isTrue(condition)) {
            fail(msg);
        }
    }

    public static void isTrue(Boolean condition) {
        isTrue(condition, null);
    }

    public static void isTrueOrNull(Boolean condition, String msg) {
        if (BooleanUtil.isFalse(condition)) {
            fail(msg);
        }
    }

    public static void isTrueOrNull(Boolean condition) {
        isTrueOrNull(condition, null);
    }

    public static void isFalse(Boolean condition, String msg) {
        if (!BooleanUtil.isFalse(condition)) {
            fail(msg);
        }
    }

    public static void isFalse(Boolean condition) {
        isFalse(condition, null);
    }

    public static void isFalseOrNull(Boolean condition, String msg) {
        if (BooleanUtil.isTrue(condition)) {
            fail(msg);
        }
    }

    public static void isFalseOrNull(Boolean condition) {
        isFalseOrNull(condition, null);
    }

    public static void notNull(Object object, String msg) {
        if (object == null) {
            fail(msg);
        }
    }

    public static void notNull(Object object) {
        notNull(object, null);
    }

    public static void isNull(Object object, String msg) {
        if (object != null) {
            fail(msg);
        }
    }

    public static void isNull(Object object) {
        isNull(object, null);
    }

    public static void notEmpty(Collection<?> collection, String msg) {
        if (CollUtil.isEmpty(collection)) {
            fail(msg);
        }
    }

    public static void notEmpty(Collection<?> collection) {
        notEmpty(collection, null);
    }

    public static <T> void notEmpty(T[] array, String msg) {
        if (ArrayUtil.hasNull(array)) {
            fail(msg);
        }
    }

    public static <T> void notEmpty(T[] array) {
        notEmpty(array, null);
    }

    public static void notEmpty(String value, String msg) {
        if (StrUtil.isEmpty(value)) {
            fail(msg);
        }
    }

    public static void notEmpty(String value) {
        notEmpty(value, null);
    }


    /**
     * @param unexpected 意想不到的值
     * @param actual     要检查的值 <code>unexpected</code>
     * @param msg
     */
    public static void notEquals(Object unexpected, Object actual, String msg) {
        if (Objects.equals(unexpected, actual)) {
            fail(msg);
        }
    }

    /**
     * @param unexpected 意想不到的值
     * @param actual     要检查的值 <code>unexpected</code>
     */
    public static void notEquals(Object unexpected, Object actual) {
        notEquals(unexpected, actual, null);
    }


    /**
     * @param expected 预期的值
     * @param actual   需要比较的字符串
     * @param msg
     */
    public static void equals(Object expected, Object actual, String msg) {
        if (!Objects.equals(expected, actual)) {
            fail(msg);
        }
    }

    /**
     * @param expected 预期的值
     * @param actual   需要比较的字符串
     */
    public static void equals(Object expected, Object actual) {
        equals(expected, actual, null);
    }


}
